<?php
require_once "ModelBase.php";

class Vote extends TTEvent_Table_Abstract
{
	protected $_name = 'tamtay_event_vote';
	protected $_primary = 'voteid';
	
	public function getEventVotes($eid, $type=1){
		//return array();
		$cond = "eid=? ";
		$params = array($eid);
		if(is_numeric($type) && $type>0){
			$cond = $cond." AND vote_type=? ";
			$params[] = $type;
		}
		$sql = "SELECT * FROM tamtay_event_vote WHERE $cond ORDER BY thid, pid";
		
		$db = $this->getSlaveDB();
		
		$rs = $db->query($sql, $params);
		
		$ret = array();
		while($obj = $rs->fetch()){
			if(!isset($ret[$obj->thid]))
				$ret[$obj->thid] = array();
			$ret[$obj->thid][$obj->pid] = $obj;
		}
		return $ret;
	}
	
	public function addThread($eid, $thread_name=""){
		$thread_name = $_POST["thread_name"];
		$sql = "SELECT MAX(thid) FROM tamtay_event_vote WHERE eid=? GROUP BY eid";
		$slaveDb = $this->getSlaveDB();
		$new_thid = $slaveDb->query($sql, $eid) + 1;
		$sql = "INSERT INTO tamtay_event_vote (eid, thid, pid, thread_name) VALUES (?, ?, ?, ?)";
		
		$masterDb = $this->getMasterDB();
		$masterDb->query($sql, array($eid, $new_thid, 1, $thread_name));
		return $new_thid;
	}
	
	public function changeThreadName($eid, $thid, $thname){
		$sql = "UPDATE tamtay_event_vote SET thread_name=? WHERE eid=? AND thid=?";
		$db = $this->getMasterDB();
		$rs = $db->query($sql, array($thname, $eid, $thid));
		if($rs)
			return $thname;
		return "";
	}
	
	public function delThread($eid, $thid){
		$sql = "DELETE FROM tamtay_event_vote WHERE eid=? AND thid=?";
		$db = $this->getMasterDB();
		if($db->query($sql, array($eid, $thid))){
			return 1;
		}
		return 0;
	}
	
	public function updateVoteList($eid, $thid, $pid){
		
		$photolist_voted = event_photo_voted($eid, $thid, $pid);
		
		$sql = "SELECT rid FROM tamtay_event_sms WHERE eid=?";
		$slaveDb = $this->getSlaveDB();
		$rs = $slaveDb->query($sql, $eid);
		$sms_vote = array();
		while($obj = $rs->fetch()){
			$sms_vote[] = $obj->rid;
		}
		$masterDb = $this->getMasterDB();
		for($i=0; $i<count($photolist_voted); $i++){
			if(in_array($photolist_voted[$i]->nid, $sms_vote)){
				$sql = "UPDATE tamtay_event_sms SET name=? WHERE nid=?";
				$masterDb->query($sql, array($photolist_voted[$i]->title, $photolist_voted[$i]->nid));
			}
			else{
				$sql = "INSERT INTO tamtay_event_sms (nid, name) VALUES(?,? )";
				$masterDb->query($sql, array($photolist_voted[$i]->nid, $photolist_voted[$i]->title));
			}
		}
	}
	
	public function inputToPhase($eid, $voteid, $ridList){
		
		if(is_array($ridList) && count($ridList)>0){
			$slaveDb = $this->getSlaveDB();
			$sql = "SELECT rid FROM tamtay_event_sms WHERE eid=? AND voteid=?";
			$rs = $slaveDb->query($sql, array($eid, $voteid));
			$sms_vote = array();
			while($obj = $rs->fetch()){
				$sms_vote[] = $obj->rid;
			}
			
			for($i=0; $i<count($ridList); $i++){
				$rid = $ridList[$i];
				$masterDb = $this->getMasterDB();
				if(in_array($rid, $sms_vote)){
					$sql = "UPDATE tamtay_event_sms SET name=? WHERE rid=? AND voteid=?";
					$masterDb->query($sql, array($_POST["name_".$rid], $rid, $voteid));
				}
				else{
					$sql = "INSERT INTO tamtay_event_sms (rid, name, voteid) VALUES(?,?,? )";
					$masterDb->query($sql, array($rid, $_POST["name_".$rid], $voteid));
				}
			}
		}
	}
	
	public function removeFromPhase($eid, $voteid, $ridList){
		
		if(is_array($ridList) && count($ridList)>0){
			$sql = "DELETE FROM tamtay_event_sms WHERE voteid=? AND rid IN (".implode(", ", $ridList).")";
			$db = $this->getMasterDB();
			$db->query($sql, $voteid);
		}
	}
	
	public function changeShortCut($eid, $short_cut){
		
		$sql = "UPDATE tamtay_event SET short_cut=? WHERE eid=?";
		$db = $this->getMasterDB();
		$db->query($sql, array($short_cut, $eid));
	}
	
	public function changeConfig($eid, $thid, $pid=0){
		
		if(is_numeric($pid) && $pid>0){
			$sql = "UPDATE tamtay_event_vote SET sms_server_ip1=?, sms_server_ip2=?, sms_server_ip3=?, " .
					"vote_start_time=?, vote_end_time=?, sms_command_code=?, " .
					"sms_destination1=?, sms_destination2=?, sms_basic_num1=?, sms_basic_num2=?, " .
					"votephoto_category=?, video_category=?, phase_name=? " .
					"WHERE eid=? AND thid=? AND pid=? ";
		}
		else{
			$sql = "INSERT tamtay_event_vote".
					"(sms_server_ip1, sms_server_ip2, sms_server_ip3, vote_start_time, vote_end_time, ".
					"sms_command_code, sms_destination1, sms_destination2, sms_basic_num1, sms_basic_num2,".
					"votephoto_category, video_category, phase_name, eid, thid, pid, thread_name)".
					" VALUES(?, ?, ?, ?, ?,".
					"?, ?, ?, ?, ?,".
					"?, ?, ? , ?, ?, ?, ?)";
		}
		$varArr = array();
		$varArr[] = $_POST["sms_server_id"][0];
		$varArr[] = $_POST["sms_server_id"][1];
		$varArr[] = $_POST["sms_server_id"][2];
		$varArr[] = ($_POST["vote_start_time"]!="")?strtotime($_POST["vote_start_time"]):0;
		$varArr[] = ($_POST["vote_end_time"]!="")?strtotime($_POST["vote_end_time"]):0;
		
		$varArr[] = $_POST["sms_command_code"];
		$varArr[] = (int)$_POST["sms_destination1"];
		$varArr[] = (int)$_POST["sms_destination2"];
		
		$varArr[] = (int)$_POST["sms_basic_num1"];
		$varArr[] = (int)$_POST["sms_basic_num2"];
		
		$varArr[] = (int)$_POST["vote_category"];
		$varArr[] = (int)$_POST["video_category"];
		$varArr[] = $_POST["phase_name"];
		
		
		
		$varArr[] = $eid;
		$varArr[] = $thid;
		if(is_numeric($pid) && $pid>0)
			$varArr[] = $pid;
		else{
			$slaveDb = $this->getSlaveDB();
			$tmp_sql = "SELECT MAX(pid) FROM tamtay_event_vote WHERE eid=? AND thid=? GROUP BY eid, thid";
			$new_pid = $slaveDb->query($tmp_sql, array($eid, $thid)) + 1;
			$varArr[] = $new_pid;
		}
		
		$varArr[] = $_POST["new_phase_thread_name"];
		$masterDb = $this->getMasterDB();
		$masterDb->query($sql, $varArr);
	}
	
	public function changePhase($eid, $thid, $pid){
		
		$sql = "UPDATE tamtay_event_vote SET ".
					" phase_name = ? WHERE eid=? AND thid=? AND pid=?";
		$varArr = array();
		$varArr[] = $_POST["phase_name"];
	
		$varArr[] = $eid;
		$varArr[] = $thid;
		$varArr[] = $pid;
	
		$masterDb = $this->getMasterDB();
		$masterDb->query($sql, $varArr);
	}
}

